<?php
/**
 * Created by PhpStorm.
 * User: ttl
 * Date: 2019/8/30
 * Time: 10:07
 */
require_once __DIR__ . '/autoloader.php';
use phpspider\core\phpspider;
use phpspider\core\requests;
use phpspider\core\selector;
use phpspider\core\db;

/* Do NOT delete this comment */
/* 不要删除这段注释 */

$configs = array(
    'name' => '好看',
    'tasknum' => 1,
    //'save_running_state' => true,
    'log_show' => true,
    'domains' => array(
        'haokan.baidu.com'
    ),
    'scan_urls' => array(
        "https://haokan.baidu.com/videoui/api/videorec?pd=videoland&act=pcFeed&tab=dongman",
    ),
    'list_url_regexes' => array(
        "https://haokan.baidu.com/tab/dongman",
    ),
    'content_url_regexes' => array(
        "https://haokan.baidu.com/v?vid=*&tab=dongman",
    ),
    'db_config' => array(
        'host'  => '127.0.0.1',
        'port'  => 3306,
        'user'  => 'root',
        'pass'  => 'jbx666',
        'name'  => 'p_test',
        'input_encoding' =>'UTF-8',
        'output_encoding'=>'UTF-8'
    ),
    'fields' => array(

    ),
);

$spider = new phpspider($configs);

$spider->on_start = function($phpspider)
{

};


//"https://haokan.baidu.com/v?vid=17898110706086256017&tab=dongman",
//        "https://haokan.baidu.com/v?vid=9781883225119252929&tab=dongman",

$spider->on_list_page = function($page, $content, $phpspider)
{
    echo 'im enter1 ='.$page['url']."<br>";

    $matches = [];
    $html = stripslashes($content);
    $preg='/<a .*?href="(.*?)".*?>/is';
    preg_match_all($preg,$html,$matches);
    for($i=0;$i<count($matches[1]);$i++)//逐个输出超链接地址
    {
        $hrefs = $matches[1][$i];
        if (strpos($hrefs, '/v?vid=') && strpos($hrefs, 'tab=dongman')) {
            $phpspider->add_url($hrefs);
            $html = requests::get($hrefs);
            $str = stripslashes($html);
            $mp4_arr = [];
            $reg = '/((http|https):\/\/)+(\w+\.)+(\w+)[\w\/\.\-]*(mp4)/';
            preg_match_all($reg, $str, $mp4_arr);
            echo json_encode($mp4_arr[0]) . "\r\n";;
            echo $hrefs . "\r\n";
        }
    }

    // 通知爬虫不再从当前网页中发现待爬url
    return false;
};

$spider->on_scan_page = function($page, $content, $phpspider) {
//    $str = "https://vd2.bdstatic.com/mda-jh1duj0cdi38hv8b/hd/mda-jh1duj0cdi38hv8b.mp4..https:\/\/eee.bdstatic.com\/mda-jh1duj0cdi38hv8b\/hd\/mda-jh1duj0cdi38hv8b.mp4?add=adsl..http://aaa.ccc.com/aaa.png";
//    $str = stripslashes($content);
//    $matches = [];
//    $reg = '/((http|https):\/\/)+(\w+\.)+(\w+)[\w\/\.\-]*(mp4)/';
//    preg_match_all($reg, $str, $matches);
//    echo json_encode($matches);


//    $str = "http:www.bbb.ccc/v?xsdwe=dongman..http:www.bbb.ccc/v?xsdwe1=dongman..http:www.bbb.ccc/v?xsdw2e=dongman";
////    $str = stripslashes($content);
//    $matches = [];
//    $reg = '[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(/.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+/.?';//'https://haokan.baidu.com/v?vid=/*/&tab=dongman';
//    preg_match_all($reg, $str, $matches);
//    echo json_encode($matches);

//    $html = stripslashes($content);
//    $data = selector::select($html, "//a@href");
//    echo $html;

//    $html = requests::get($page['url']);
//    echo $html;

//    echo strpos("You love php, I love php too!","You love php");
//    echo "\r\n";
    $db_config = $phpspider->get_config("db_config");
    // 数据库连接
    db::set_connect('default', $db_config);
    db::_init();

    $to_url = 'https://haokan.baidu.com/videoui/api/videorec?pd=videoland&act=pcFeed&tab=dongman';
    for($i=0;$i<2;$i++) {

        $html = requests::get($to_url);
        $data = json_decode($html, true);
        $videos = $data['data']['response']['videos'];

        foreach ($videos as $k => $v) {

            $source_name = $v['source_name'];
            $sql_str = "select * from p_test where pname = '".$source_name."'";
            $hmark = db::get_one($sql_str);
//            echo  '$hmark='.$hmark.'$source_name='.$source_name.'sql='.$sql_str."\r\n";
            if(empty($hmark)){
                $markid = db::insert('p_test', ['pname' => $v['source_name'], 'pmsg' => $v['play_url']]);
                echo 'url='.$v['play_url'] . "\r\n";
                download($v['play_url'],$source_name);
            }
        }

        sleep(3);
    }



    $matches = [];
    $html = stripslashes($content);
    $preg='/<a .*?href="(.*?)".*?>/is';
    preg_match_all($preg,$html,$matches);
    for($i=0;$i<count($matches[1]);$i++)//逐个输出超链接地址
    {
        $hrefs = $matches[1][$i];
        if (strpos($hrefs, '/v?vid=') && strpos($hrefs, 'tab=dongman')){
            $phpspider->add_url($hrefs);
            echo $hrefs . "\r\n";
        }
    }

    // 通知爬虫不再从当前网页中发现待爬url
    return false;
};


$spider->on_content_page = function($page, $content, $phpspider)
{
    echo 'this1='.$page['url'];

//    $html = requests::get($page['url']);
//    $data = selector::select($content, "//a@href");
//    echo json_encode($data);

    return false;
};

$spider->on_extract_page = function($page, $data)
{
    echo 'abbccddee';

    echo 'page=';
    echo $data;
};


$spider->start();

function download($url,$file_name, $path = 'video/')
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    $file = curl_exec($ch);
    curl_close($ch);

    if (!file_exists($path)) {
        mkdir($path);
    }

//    $filename = pathinfo($url, PATHINFO_BASENAME).$file_name;
    $filename = $file_name.'.mp4';
    $resource = fopen($path . $filename, 'a');
    fwrite($resource, $file);
    fclose($resource);
    return $filename;
}